package com.groupfifteen.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.groupfifteen.entity.SysExamManageEntity;
import org.apache.ibatis.annotations.*;

@Mapper
public interface SysExamManageDao extends BaseMapper<SysExamManageEntity> {
    @Select("select * from sys_exam_manage")
    IPage<SysExamManageEntity> findAll(Page page);

    /**
     * 通过考试编号查询
     *
     * @param examCode 考试编号
     * @return 考试信息
     */
    @Select("select * from sys_exam_manage where exam_code = #{examCode}")
    SysExamManageEntity findById(Integer examCode);

    /**
     * 根据考试编号删除
     *
     * @param examCode 考试编号
     */
    @Delete("delete from sys_exam_manage where exam_code = #{examCode}")
    int delete(Integer examCode);

    @Update("update sys_exam_manage set description = #{description},source = #{source},paper_id = #{paperId}," +
            "exam_date = #{examDate},total_time = #{totalTime},grade = #{grade},term = #{term}," +
            "major = #{major},institute = #{institute},total_score = #{totalScore}," +
            "type = #{type},tips = #{tips} where exam_code = #{examCode}")
    int update(SysExamManageEntity exammanage);

    @Options(useGeneratedKeys = true, keyProperty = "examCode")
    @Insert("insert into sys_exam_manage(description,source,paper_id,exam_date,total_time,grade,term,major,institute," +
            "total_score,type,tips)" +
            " values(#{description},#{source},#{paperId},#{examDate},#{totalTime},#{grade},#{term},#{major}," +
            "#{institute},#{totalScore},#{type},#{tips})")
    int add(SysExamManageEntity exammanage);

    /**
     * 查询最后一条记录的paperId,返回给前端达到自增效果
     *
     * @return paperId
     */
    @Select("select paper_id from sys_exam_manage order by paper_id desc limit 1")
    SysExamManageEntity findOnlyPaperId();
}
